using Firestarter.Test.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using FireStarterModeller.UI.Relationship;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Relationships
{
    [TestFixture]
    public class TestDMRelationshipEditorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GetControlFactory();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.AreSame(controlFactory, relationshipEditorControl.ControlFactory);
        }

        [Test]
        public void Test_Layout()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //---------------Test Result -----------------------
            Assert.AreEqual(2, relationshipEditorControl.Controls.Count);
            AssertContainsDMRelationshipPanelInfoControl(relationshipEditorControl);
            DMRelationshipPanelInfoControl panelInfoControl = GetDMRelationshipPanelInfoControl(relationshipEditorControl);
            Assert.AreEqual(1, panelInfoControl.Controls.Count);
            Assert.IsNotNull(relationshipEditorControl.RelationshipPanelInfo);
            Assert.IsNotNull(relationshipEditorControl.RelationshipPropertiesControl);
            AssertContainsDMRelatedPropertiesControl(relationshipEditorControl);
        }

        private static void AssertContainsDMRelationshipPanelInfoControl(DMRelationshipEditorControl relationshipEditorControl)
        {
            bool foundGroupBox = (GetDMRelationshipPanelInfoControl(relationshipEditorControl) != null);
            Assert.IsTrue(foundGroupBox, "The Group box should have been found in the Controls collection");
        }

        private static DMRelationshipPanelInfoControl GetDMRelationshipPanelInfoControl(DMRelationshipEditorControl relationshipEditorControl)
        {
            foreach (object control in relationshipEditorControl.Controls)
            {
                if (control is DMRelationshipPanelInfoControl)
                {
                    return (DMRelationshipPanelInfoControl)control;
                }
            }
            return null;
        }

        private static void AssertContainsDMRelatedPropertiesControl(DMRelationshipEditorControl relationshipEditorControl)
        {
            bool foundRelationshipProperties = (GetDMRelatedPropertiesControl(relationshipEditorControl) != null);
            Assert.IsTrue(foundRelationshipProperties, "The DMRelatedPropertiesControl should have been found in the Controls collection");
        }

        private static DMRelatedPropertiesControl GetDMRelatedPropertiesControl(DMRelationshipEditorControl relationshipEditorControl)
        {
            foreach (object control in relationshipEditorControl.Controls)
            {
                if (control is DMRelatedPropertiesControl)
                {
                    return (DMRelatedPropertiesControl) control;
                }
            }
            return null;
        }

        [Test]
        public void Test_BusinessObject_WhenSet_ShouldSetOnPanelAndGrid()
        {
            //---------------Set up test pack-------------------
            DMRelationship newBO = new DMRelationship();
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //---------------Assert Precondition----------------
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPanelInfo.BusinessObject);
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPropertiesControl.BusinessObject);
            //---------------Execute Test ----------------------
            relationshipEditorControl.BusinessObject = newBO;
            //---------------Test Result -----------------------
            Assert.AreSame(newBO, relationshipEditorControl.BusinessObject);
            Assert.AreSame(newBO, relationshipEditorControl.RelationshipPanelInfo.BusinessObject);
            Assert.AreSame(newBO, relationshipEditorControl.RelationshipPropertiesControl.BusinessObject);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_ShouldCreateNewSortOrder_WhenNoExistingSortOrder()
        {
            //---------------Set up test pack-------------------
            DMRelationship newBO = new DMRelationship();
            newBO.DMSortOrder = null;
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //---------------Assert Precondition----------------
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPanelInfo.BusinessObject);
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPropertiesControl.BusinessObject);
            //---------------Execute Test ----------------------
            relationshipEditorControl.BusinessObject = newBO;
            //---------------Test Result -----------------------
            Assert.AreSame(newBO, relationshipEditorControl.BusinessObject);
            Assert.IsNotNull(newBO.DMSortOrder);
            Assert.IsTrue(newBO.Status.IsNew);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_ShouldNotCreateNewSortOrder_WhenHasExistingSortOrder()
        {
            //---------------Set up test pack-------------------
            DMRelationship newBO = new DMRelationship();
            DMSortOrder dmSortOrder = new DMSortOrder();
            newBO.DMSortOrder = dmSortOrder;
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //---------------Assert Precondition----------------
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPanelInfo.BusinessObject);
            Assert.AreEqual(relationshipEditorControl.BusinessObject, relationshipEditorControl.RelationshipPropertiesControl.BusinessObject);
            Assert.IsNotNull(newBO.DMSortOrder);
            //---------------Execute Test ----------------------
            relationshipEditorControl.BusinessObject = newBO;
            //---------------Test Result -----------------------
            Assert.AreSame(newBO, relationshipEditorControl.BusinessObject);
            Assert.AreSame(dmSortOrder,newBO.DMSortOrder);
        }

        [Ignore("Changing how this works")]
        [Test]
        public void Test_DisplayErrors()
        {
            ////---------------Set up test pack-------------------
            //DMRelationshipEditorControl dmClassEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //DMClass newBO = new DMClass();
            //dmClassEditorControl.BusinessObject = newBO;
            //PanelInfo.FieldInfo fieldInfo = dmClassEditorControl.DetailPanelInfo.FieldInfos["ClassNameBO"];
            ////---------------Assert Precondition----------------
            //Assert.IsFalse(fieldInfo.ControlMapper.ErrorProvider.GetError(fieldInfo.InputControl).Length > 0);

            ////---------------Execute Test ----------------------
            //dmClassEditorControl.DisplayErrors();

            ////---------------Test Result -----------------------
            //Assert.IsTrue(fieldInfo.ControlMapper.ErrorProvider.GetError(fieldInfo.InputControl).Length > 0);
        }

        [Ignore("Changing how this works")]
        [Test]
        public void Test_ClearErrors()
        {
            ////---------------Set up test pack-------------------
            //DMRelationshipEditorControl dmRelationshipEditorControl = new DMRelationshipEditorControl(GetControlFactory());
            //DMClass newBO = new DMClass();
            //dmRelationshipEditorControl.BusinessObject = newBO;
            //PanelInfo.FieldInfo fieldInfo = dmRelationshipEditorControl.DetailPanelInfo.FieldInfos["ClassNameBO"];
            //dmRelationshipEditorControl.DisplayErrors();

            ////---------------Assert Precondition----------------
            //Assert.IsTrue(fieldInfo.ControlMapper.ErrorProvider.GetError(fieldInfo.InputControl).Length > 0);

            ////---------------Execute Test ----------------------
            //dmRelationshipEditorControl.ClearErrors();

            ////---------------Test Result -----------------------
            //Assert.IsFalse(fieldInfo.ControlMapper.ErrorProvider.GetError(fieldInfo.InputControl).Length > 0);
        }

        [Test]
        [Ignore("This is for visual testing purposes")]
        public void Test_Visually()
        {
            //---------------Set up test pack-------------------
            TestUtilsDomain.WaitForGC();
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMClass dmClass = TestUtilsDMClass.CreateSavedDMClass("a11111111");
            DMRelationship relationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMClass otherClass = TestUtilsDMClass.CreateSavedDMClass("a11111111112");

            IControlFactory controlFactory = GetControlFactory();
            DMRelationshipEditorControl relationshipEditorControl = new DMRelationshipEditorControl(controlFactory);
            relationshipEditorControl.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(controlFactory, relationshipEditorControl, Habanero.Faces.Base.DockStyle.Fill);
            //---------------Test Result -----------------------
        }
    }
}